<?php

Loader::using("Models::Entities::Buques");

/**
 * Description of AdministrarBuques
 *
 * @author JeyDotC
 * 
 * @export Modules
 * @export-metadata RequiredRoles:Agencia
 */
class AdministrarBuques extends Module {

    private $maxBuques = 100;

    /**
     *
     * @var Buques 
     */
    private $buque;

    protected function onModuleReady() {
        $this->buque = new Buques();
    }

    public function index() {
        $pagina = $this->request->get("page", 0);
        $buques = $this->components->Buques->listar($pagina * $this->maxBuques, $this->maxBuques);

        $this->assign("newURL", $this->route->action("create"));
        $this->assign("buques", $buques);

        $this->display("index");
    }

    function view($id = null) {
        $id = $id ? $id : $this->request->get("id");

        /* @var $buque Buques */
        $buque = $this->components->Buques->getById($id);

        $this->assign("buque", $buque);
        $this->assign("editUrl", $this->route->action("edit", array("id" => $buque->getIdBuque(),)));
        $this->assign("deleteUrl", $this->route->action("remove", array("id" => $buque->getIdBuque(),)));

        $this->display("view");
    }

    function create() {
        $this->setupEditor("save");
        $this->display("create");
    }

    function edit() {
        $id = $this->request->get("id");
        $this->buque = $this->components->Buques->getById($id);

        $this->setupEditor("update", "view", array("id" => $id,));
        $this->display("edit");
    }

    function save() {
        if ($this->request->submitted()) {

            $this->request->bind($this->buque, true);

            /* @var $newBuque Buques */
            $newBuque = $this->components->Buques->create($this->buque);
            
            $this->addMessage(new Message("", "Buque creado correctamente", Message::SUCCESS));
            $this->view($newBuque->getIdBuque());
        } else {
            $this->create();
        }
    }

    function update() {
        if ($this->request->submitted()) {

            $this->request->bind($this->buque, true);

            $this->components->Buques->update($this->buque);

            $this->addMessage(new Message("", "Buque actualizado", Message::SUCCESS));
            $this->view($this->buque->getIdBuque());
        } else {
            $this->edit();
        }
    }

    function remove() {
        $id = $this->request->get("id");

        $this->addMessage(new Message("", "Buque eliminado", Message::SUCCESS));
        $this->components->Buques->remove($id);

        $this->index();
    }

    private function setupEditor($action, $backAction = "index",
            $backActionParams = array()) {
        $lineas = $this->components->Lineas->getAll();
        $paises = $this->components->Paises->getAll();

        $this->assign("back", $this->route->action($backAction, $backActionParams));
        $this->assign("buque", $this->buque);
        $this->assign("action", $this->route->action($action));
        $this->assign("lineas", $lineas);
        $this->assign("paises", $paises);
    }

}

?>
